package com.lishu.carsafe.adapter;

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.RequiresApi;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.lishu.carsafe.R;
import com.lishu.carsafe.activity.MonitorRealTimeActivity;
import com.lishu.carsafe.model.MonitorListModel;
import com.lishu.carsafe.model.MonitorListModel.MonitorBean;

import java.util.List;

/**
 * describe:
 *
 * @author:ZhangWei(张伟) date:2020/6/4
 * time:18:02
 * motto:高山仰止，景行行止，虽不能至，心向往之。
 */
public class MonitorAdapter extends RecyclerView.Adapter<MonitorAdapter.MonitorViewHolder> {
    private List<MonitorBean> list;
    private LayoutInflater inflater;
    private Context context;

    public MonitorAdapter(Context context) {
        this.context = context;
        inflater = LayoutInflater.from(context);
    }

    public void setList(List<MonitorBean> list) {
        this.list = list;
        notifyDataSetChanged();
    }

    @NonNull
    @Override
    public MonitorViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
        View view = inflater.inflate(R.layout.activity_monitor_item, viewGroup, false);
        return new MonitorViewHolder(view);
    }

    @RequiresApi(api = Build.VERSION_CODES.JELLY_BEAN)
    @Override
    public void onBindViewHolder(@NonNull MonitorViewHolder holder, int i) {
        final MonitorBean bean = list.get(i);
        switch (bean.getState()) {
            case 0://离线
                holder.iv_monitor.setImageResource(R.mipmap.icon_monitor_off_line);
                holder.tv_monitor_line.setText("离线");
                break;
            case 1://在线
                holder.iv_monitor.setImageResource(R.mipmap.icon_monitor_on_line);
                holder.tv_monitor_line.setText("在线");
                break;
        }
        setBackground(holder, i);
        holder.tv_monitor_car.setText(bean.getVehicleId().getNumber() + " - " + bean.getVehicleId().getName());
        holder.tv_monitor_line_name.setText(bean.getLineId().getName());
        Log.i("zw_test", "url=" + bean.getVehicleId().getPictureUrl());

        holder.tv_monitor.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //跳转到对应的界面
                skipActivity(bean.getState(),bean.getVehicleId().getId());
            }
        });
    }

    @SuppressLint("NewApi")
    private void setBackground(MonitorViewHolder holder, int i) {
        if (i % 3 == 0) {
            holder.rl_monitor.setBackground(context.getResources().getDrawable(R.mipmap.icon_monitor_blue));
            holder.iv_monitor_car.setImageResource(R.mipmap.icon_monitor_blue_car);
        } else if (i % 3 == 1) {
            holder.rl_monitor.setBackground(context.getResources().getDrawable(R.mipmap.icon_monitor_green));
            holder.iv_monitor_car.setImageResource(R.mipmap.icon_monitor_green_car);
        } else if (i % 3 == 2) {
            holder.rl_monitor.setBackground(context.getResources().getDrawable(R.mipmap.icon_monitor_red));
            holder.iv_monitor_car.setImageResource(R.mipmap.icon_monitor_red_car);
        }
    }

    @Override
    public int getItemCount() {
        return list == null ? 0 : list.size();
    }

    public static class MonitorViewHolder extends RecyclerView.ViewHolder {
        RelativeLayout rl_monitor;

        ImageView iv_monitor_car;//车辆图标 蓝 绿 红
        TextView tv_monitor_car;
        TextView tv_monitor_line_name;
        TextView tv_monitor_line;
        ImageView iv_monitor;
        TextView tv_monitor;

        public MonitorViewHolder(@NonNull View itemView) {
            super(itemView);
            rl_monitor = itemView.findViewById(R.id.rl_monitor);
            iv_monitor_car = itemView.findViewById(R.id.iv_monitor_car);
            tv_monitor_line_name = itemView.findViewById(R.id.tv_monitor_line_name);
            tv_monitor_car = itemView.findViewById(R.id.tv_monitor_car);
            tv_monitor_line = itemView.findViewById(R.id.tv_monitor_line);
            iv_monitor = itemView.findViewById(R.id.iv_monitor);
            tv_monitor = itemView.findViewById(R.id.tv_monitor);
        }
    }

    /**
     * 跳转到告警信息界面
     *
     * @param state
     */
    private void skipActivity(int state,String vehicleId) {
        Intent intent = new Intent(context, MonitorRealTimeActivity.class);
        intent.putExtra(MonitorRealTimeActivity.MONITOR_STATE, state);
        intent.putExtra(MonitorRealTimeActivity.MONITOR_VEHICLE_ID, vehicleId);
        context.startActivity(intent);
    }
}
